package ro.sergiu.finder.utils;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Date;

/**
 *
 * @author Toporjinschi
 */
public final class Tools {

    private Tools() {
    }

    public static void setStatementBasedOnType(PreparedStatement stmt, Object value) throws SQLException {
        int parmLevel = stmt.getParameterMetaData().getParameterCount();
        if (value.getClass() == String.class) {
            stmt.setString(parmLevel, (String) value);
        } else if (value.getClass() == Integer.class) {
            stmt.setInt(parmLevel, Integer.valueOf(String.valueOf(value)));
        } else if (value.getClass() == Float.class) {
            stmt.setFloat(parmLevel, Float.valueOf(String.valueOf(value)));
        } else if (value.getClass() == Double.class) {
            stmt.setDouble(parmLevel, Double.valueOf(String.valueOf(value)));
        } else if (value.getClass() == Long.class) {
            stmt.setLong(parmLevel, Long.valueOf(String.valueOf(value)));
        } else if (value.getClass() == Short.class) {
            stmt.setShort(parmLevel, Short.valueOf(String.valueOf(value)));
        } else if (value.getClass() == Byte.class) {
            stmt.setByte(parmLevel, Byte.valueOf(String.valueOf(value)));
        } else if (value.getClass() == Date.class) {
            stmt.setDate(parmLevel, new java.sql.Date(((Date) value).getTime()));
        } else {
            throw new IllegalArgumentException("Unknown type " + value.getClass().getName());
        }
    }
}
